namespace RealDolmen.Helpers
{
	/// <summary>
	/// Summary description for CapacityFormatter.
	/// </summary>
	public class CapacityFormatter
	{
		#region Variables
		private static long KILOBYTE;
		private static long MEGABYTE;
		private static long GIGABYTE;
		private static long TERABYTE;
		#endregion // Variables

		#region Constructor
		static CapacityFormatter()
		{
			KILOBYTE = 1024;
			MEGABYTE = 1024 * KILOBYTE;
			GIGABYTE = 1024 * MEGABYTE;
			TERABYTE = 1024 * GIGABYTE;
		}
		#endregion //Constructor
		
		#region Business Methods
		/// <summary>
		/// Format bytes into human readable string.
		/// </summary>
		/// <param name="bytes">Long with number of bytes.</param>
		/// <returns>Returns number of bytes in appropriate order.</returns>
		public static string FormatBytes(long bytes)
		{	
			if(bytes < KILOBYTE)
				return string.Format("{0} B", bytes);
			else if(bytes < MEGABYTE)
				return string.Format("{0} KB", ((double)bytes/KILOBYTE).ToString("####.##"));
			else if(bytes < GIGABYTE)
				return string.Format("{0} MB", ((double)bytes/MEGABYTE).ToString("####.##"));
			else if(bytes < TERABYTE)
				return string.Format("{0} GB", ((double)bytes/GIGABYTE).ToString("####.##"));

			return "ERROR";
		}
		#endregion // Business Methods
	}
}
